package www.woniuxy.com.class112.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileTypeUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import www.woniuxy.com.class112.dto.KnowledgeBaseDto;
import www.woniuxy.com.class112.dto.PolicyDto;
import www.woniuxy.com.class112.form.policyForm.AddPolicyForm;
import www.woniuxy.com.class112.form.policyForm.PolicyCompileFORM;
import www.woniuxy.com.class112.form.policyForm.PolicyfdAllForm;
import www.woniuxy.com.class112.param.policyParam.AddPolicyParam;
import www.woniuxy.com.class112.param.policyParam.PolicyCompileParam;
import www.woniuxy.com.class112.param.policyParam.PolicyfdAllParam;
import www.woniuxy.com.class112.service.PolicyService;
import www.woniuxy.com.class112.util.JwtUtil;
import www.woniuxy.com.class112.util.Result;

import javax.annotation.Resource;

@RestController
@RequestMapping("/policy")
@Api(tags = "政策相关接口")
public class PolicyController {
    @Resource
    private PolicyService policyService;
    @PostMapping("/policyfdAll")
    @ApiOperation("查询所有政策")
    public Result policyfdAll(@RequestBody PolicyfdAllForm form)throws Exception{
        PolicyfdAllParam policyfdAllParam1 = BeanUtil.toBean(form, PolicyfdAllParam.class);
        PageInfo<PolicyDto> policyDtos = policyService.policyfdAll(policyfdAllParam1);
        return Result.success("查询成功",policyDtos);
    }
    @PostMapping("/userpolicyfdAll")
    @ApiOperation("用户查询所有政策")
    public Result userpolicyfdAll(@RequestBody PolicyfdAllForm form)throws Exception{
        PolicyfdAllParam policyfdAllParam1 = BeanUtil.toBean(form, PolicyfdAllParam.class);
        PageInfo<PolicyDto> policyDtos = policyService.userpolicyfdAll(policyfdAllParam1);
        return Result.success("查询成功",policyDtos);
    }
    @GetMapping("/delistpolicy")
    @ApiOperation("下架政策")
    public Result delistpolicy(Long policyId)throws Exception{
        policyService.delistpolicy(policyId);
        return Result.success("下架成功",null);
    }
    @GetMapping("/enablepolicy")
    @ApiOperation("发布政策")
    public Result enablepolicy(Long policyId)throws Exception{
        policyService.enablepolicy(policyId);
        return Result.success("发布政策成功",null);
    }
    @GetMapping("/deletepolicy")
    @ApiOperation("删除政策")
    public Result deletepolicy(Long policyId)throws Exception{
        policyService.deletepolicy(policyId);
        return Result.success("删除策成功",null);
    }
    @PostMapping("/addpolicy")
    @ApiOperation("添加政策")
    public Result addpolicy(@RequestBody AddPolicyForm form,@RequestHeader String token)throws Exception{
        System.out.println(form);
        Long userId = JwtUtil.getUserId(token);
        AddPolicyParam param = BeanUtil.toBean(form, AddPolicyParam.class);
        param.setCreaterId(userId);
        policyService.addpolicy(param);
        return Result.success("添加政策成功",null);
    }
    @PostMapping("/policyCompile")
    @ApiOperation("修改政策")
    public Result compilepolicy(@RequestBody PolicyCompileFORM form)throws Exception{
        PolicyCompileParam param = BeanUtil.toBean(form, PolicyCompileParam.class);
        policyService.compilepolicy(param);
        return Result.success("修改政策成功",null);
    }
    @GetMapping("/tapNUm")
    @ApiOperation("添加点击量")
    public Result tapNUm(Long id)throws Exception{
        policyService.tapNUm(id);
        return Result.success("添加点击量成功",null);
    }
    @PostMapping("/Pictureupload")
    @ApiOperation("测试图片上传")
    public Result Pictureupload(MultipartFile imageUrl)throws Exception{
        String type = FileTypeUtil.getType(imageUrl.getInputStream());
        if(type.equals("jpg")||type.equals("png")){
        }else{
            return Result.fail("上传文件类型错误",null);
        }
        String s=policyService.Pictureupload(imageUrl);
        return Result.fail("图片上传阿里云成功",s);
    }
}
